package lu.med.praxiswahl.wahlumed.client.utilities;

/**
 * 
 * @author Lars Holger Roth
 */
public class Parser {
	public static String[] parseString(String str) {
		if ((str == null) || (str.equals(""))) {
			String[] r = new String[1];
			r[0] = "";
			return r;
		}
		String[] ret;
		// Ermittelt die Anzahl der Parameter in dem String
		// Jene entsprechen der Anzahl Trennzeichen +1
		// x#y#z#a#b usw
		char[] strc = str.toCharArray();
		int count = 0;
		for (int i = 0; i < strc.length; i++) {
			if (strc[i] == '#')
				count++;
		}
		count++;
		ret = new String[count];

		for (int i = 0; i < ret.length; i++) {
			ret[i] = "";
		}

		int curIndex = 0;

		for (int i = 0; i < strc.length; i++) {
			if (strc[i] == '#') {
				curIndex++;
			} else {
				ret[curIndex] += strc[i];
			}
		}

		return ret;
	}

	public static String[] parseString(String str, char key) {
		String[] ret;
		// Ermittelt die Anzahl der Parameter in dem String
		// Jene entsprechen der Anzahl Trennzeichen +1
		// x#y#z#a#b usw
		char[] strc = str.toCharArray();
		int count = 0;
		for (int i = 0; i < strc.length; i++) {
			if (strc[i] == key)
				count++;
		}
		count++;
		ret = new String[count];

		for (int i = 0; i < ret.length; i++) {
			ret[i] = "";
		}

		int curIndex = 0;

		for (int i = 0; i < strc.length; i++) {
			if (strc[i] == key) {
				curIndex++;
			} else {
				ret[curIndex] += strc[i];
			}
		}

		return ret;
	}

	public static String[] parseString(String str, char key1, char key2) {
		String[] ret;
		// Ermittelt die Anzahl der Parameter in dem String
		// Jene entsprechen der Anzahl Trennzeichen +1
		// x#y#z#a#b usw
		char[] strc = str.toCharArray();
		int count = 0;
		for (int i = 0; i < strc.length; i++) {
			if (strc[i] == key1)
				count++;
		}
		ret = new String[count];

		for (int i = 0; i < ret.length; i++) {
			ret[i] = "";
		}

		int curIndex = 0;

		for (int i = 1; i < strc.length; i++) {
			if (strc[i] == key1) {
				curIndex++;
			} else if (strc[i] != key2) {
				if (curIndex < ret.length)
					ret[curIndex] += strc[i];
			}
		}

		return ret;
	}

	public static String transformStringOfBrackets(String str) {
		// Wandelt () in {} um
		if (str == null)
			return "";
		String ret = str.replace('(', '{');
		ret = ret.replace(')', '}');
		return ret;
	}

	public static String transformStringOfReturn(String str) {
		String b = "";
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == '\n')
				b += "\\";
			else
				b += str.charAt(i);
		}
		return b;
	}

	public static String transformStringToBrackets(String str) {
		// Wandelt {} in () um
		if (str == null)
			return "";
		String ret = str.replace('{', '(');
		ret = ret.replace('}', ')');
		return ret;
	}

	public static String transformStringToReturn(String str) {
		String b = "";
		for (int i = 0; i < str.length(); i++) {
			if (str.charAt(i) == '\\')
				b += "\n";
			else if (str.charAt(i) == '~')
				b += "\n";
			else
				b += str.charAt(i);
		}
		return b;
	}
}
